var imagesUpload = {
	init: function(fileUpload) {
		fileUpload = $(fileUpload);
		fileUpload.hide();
		
		var container = $('<div class="images-upload-container" />');
		fileUpload.after(container);
		
		var link = $('<a href="#" class="blue-btn">Выбрать изображения</a>');
		container.append(link);
		
		link.click(function() {
			fileUpload.click();
			return false;
		});
		
		fileUpload.change(function() {
			imagesUpload.draw(fileUpload.prop('files'));
		});
	},
	
	draw: function(files) {
		var container = $('.images-upload-container');
		$('.photos', container).remove();		
		container.prepend('<div class="photos" />');
		
		$.each(files, function(i, file) {
			if (!file.type.match(/image.*/)) return true;
			
			var div = $('<div class="photo" />').appendTo($('.photos', container));
			var img = $('<img />').appendTo(div);
			
			var reader = new FileReader();
		    reader.onload = (function(aImg) {
		    	return function(e) {
		    		aImg.attr('src', e.target.result);
		    	};
		    })(img);
		      
		    reader.readAsDataURL(file);
		});
	}
};

$('#images-upload').livequery(function() {
	imagesUpload.init(this);
});